Shuttle
Shuttle is a library for testing concurrent Rust code. It is an implementation of a number of randomized concurrency testing techniques, including A Randomized Scheduler with Probabilistic Guarantees of Finding Bugs.
Getting started
Consider this simple piece of concurrent code:
use ;
use thread;
let lock = new;
let lock2 = lock.clone;
spawn;
assert_eq!;
There is an obvious race condition here: if the spawned thread runs before the assertion, the assertion will fail. But writing a unit test that finds this execution is tricky. We could run the test many times and try to "get lucky" by finding a failing execution, but that's not a very reliable testing approach. Even if the test does fail, it will be difficult to debug: we won't be able to easily catch the failure in a debugger, and every time we make a change, we will need to run the test many times to decide whether we fixed the issue.
Randomly testing concurrent code with Shuttle
Shuttle avoids this issue by controlling the scheduling of each thread in the program, and scheduling those threads randomly. By controlling the scheduling, Shuttle allows us to reproduce failing tests deterministically. By using random scheduling, with appropriate heuristics, Shuttle can still catch most (non-adversarial) concurrency bugs even though it is not an exhaustive checker.
A Shuttle version of the above test just wraps the test body in a call to Shuttle's
check_random
function, and replaces the concurrency-related imports from std
with imports
from shuttle
:
use ;
use thread;
check_random;
This test detects the assertion failure with extremely high probability (over 99.9999%).
Shuttle is inspired by the Loom library for testing concurrent Rust code. Shuttle focuses on randomized testing, rather than the exhaustive testing that Loom offers. This is a soundness—scalability trade-off: Shuttle is not sound (a passing Shuttle test does not prove the code is correct), but it scales to much larger test cases than Loom. Empirically, randomized testing is successful at finding most concurrency bugs, which tend not to be adversarial.
License
This project is licensed under the Apache-2.0 License.
Security
See CONTRIBUTING for more information.